// pages/personcenter/personcenter.js
const app = getApp()
Page({

  data: {

    headurl: '',
    username: '',
    mark:'',
    tel:'',
  },

  onLoad: function (options) {
    console.log(wx.getStorageSync('token'))
    if (wx.getStorageSync('token') ){ 
      this.getinfo()
      return;  
      if (wx.getStorageSync('headurl') || wx.getStorageSync('username') || wx.getStorageSync('mark') || wx.getStorageSync('tel')) {
        this.setData({
          headurl: wx.getStorageSync('headurl'),
          username: wx.getStorageSync('username'),
          mark: wx.getStorageSync('mark'),
          tel: wx.getStorageSync('tel'),
        });
      }else{
        // this.getinfo()
      }
    }
    
  },
  onShow(){
    console.log(wx.getStorageSync('tel'))
    if (wx.getStorageSync('tel')){
      this.setData({
        tel: wx.getStorageSync('tel')
      })
    }
    if (wx.getStorageSync('token')){
      this.getinfo()
    }
    // if (app.globalData.isLogin) {
    //   this.getinfo()
    // }
  },
  getinfo(){   //获取基本信息
  let that =this
    wx.request({
      url: app.globalData.url + '/jxb/oneself/getUserInfo',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function (res) {
        console.log(res)
        if(res.data.code==0){
          that.setData({
            headurl: res.data.data.avatar,
            username: res.data.data.username,
            mark: res.data.data.remark,
            // tel: res.data.data.tel
          });
          wx.setStorageSync('headurl', res.data.data.avatar);
          wx.setStorageSync('username', res.data.data.username);
          wx.setStorageSync('mark', res.data.data.remark);
          // wx.setStorageSync('tel', res.data.data.tel);
        }else if(res.data.code==401){
          that.setData({
            headurl: '',
            username:'',
            mark: '',
            tel: ''
          }) 
        }
        
      }
    })
  },
 login: function () {   //登录
    wx.showLoading({
      title:'登录中...'
    });
    let that = this;
    wx.login({
      success: res => {
        if (res.code) {  //有code
          app.globalData.code = res.code;
          wx.getSetting({
            success: function (res) {  //已授权
              if (res.authSetting['scope.userInfo']) {
                wx.getUserInfo({  //调用用户信息
                  success: res => { //获取信息   
                    wx.setStorageSync('headurl', res.userInfo.avatarUrl);
                    wx.setStorageSync('username', res.userInfo.nickName);
                    wx.request({   //传后台
                      url: app.globalData.url + '/jxb/login/login/loginAuth',
                      method: 'POST',
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      data: {
                        code: app.globalData.code,
                        encryptedData: res.encryptedData,
                        iv: res.iv
                      },
                      success: function (res3) {
                        if(res3.data.code==0){
                          wx.hideLoading();
                          wx.setStorageSync('token', res3.data.data.token);
                          wx.setStorageSync('uid', res3.data.data.uid);
                          app.globalData.isLogin = true;
                          that.getinfo();
                          wx.showToast({
                            title: res3.data.msg,
                            duration: 2000,
                            icon: 'none',
                          })
                        }else{
                          wx.showToast({
                            title: res3.data.msg,
                            duration: 2000,
                            icon: 'none',
                          })
                        }
                       
                      },

                    })
                  }
                })
              } else {  //未授权
            
                 // that.getUserInfo();
              }
            }

          });// 查看授权

        } else {     //没有code

        }
      }
    })
  },
  getUserInfo(res) {       //授权
    console.log(res.detail.userInfo)
    if (res.detail.userInfo) {
      console.log("点击了同意授权");
      this.login();
    } else {
      console.log("点击了拒绝授权");
    }
  },
  goeditmsg:function(){  //修改头像
    if (wx.getStorageSync('token') ){
      wx.navigateTo({
        url: '/pages/personcenter/editmessage/editmessage?headurl=' + this.data.headurl + '&username=' + this.data.username + '&mark=' + this.data.mark + '&tel=' + this.data.tel,
      })
    }else {
      wx.showToast({
        title: '亲，请先登录',
        duration: 2000,
        icon: 'none',
      })
    }

  },
  goadress:function(){  //地址管理
    if (wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/adress/adress',
      })
    }else{
      wx.showToast({
        title: '亲，请先登录',
        duration:2000,
        icon: 'none',
      })
    }
  },
  gomyorder:function(){  //我的订单
    if (wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/myorder/myorder',
      })
    } else {
      wx.showToast({
        title: '亲，请先登录',
        duration: 2000,
        icon: 'none',
      })
    } 
  },
  goyouhuicard:function(){ //优惠券
    if (wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/youhuicars/youhuicars',
      })
    } else {
      wx.showToast({
        title: '亲，请先登录',
        duration: 2000,
        icon: 'none',
      })
    } 
  },
  gohelp(){ //帮助中心
    if (wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/help/help',
      })
    } else {
      wx.showToast({
        title: '亲，请先登录',
        icon:'none',
        duration: 2000,
      })
    }
  },
  getPhoneNumber(e){
    var data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      token: wx.getStorageSync('token'),
    }
    let taht =this;
    if (e.detail.errMsg =='getPhoneNumber:ok'){
      wx.request({
        method: "post",
        url: app.globalData.url + '/jxb/login/login/getPhoneNumber',
        data: data,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data)
          wx.setStorageSync('tel') 
          taht.setData({
            tellogin: true,
            tel: res.data.data.tel,
          })
          taht.saveinfo('', res.data.data.tel,'')
          var phoneNum = res.data;
          wx.hideLoading()
          wx.setStorage({
            key: 'tel',
            data: res.data.data.tel,
          })
          wx.showToast({
            title: '授权成功',
            icon: 'success',
            duration: 2000
          })
        },
        fail: function (res) {
          console.log(res)
        }
      })

    }

  },
  saveinfo(remark, tel, avatar) { //保存信息
    wx.request({
      url: app.globalData.url + '/jxb/oneself/updateUserInfo',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        remark: remark,
        tel: tel,
        avatar: avatar,
      },
      success: function (res) {
        // wx.showToast({
        //   title: '修改成功',
        //   icon: 'success',
        //   duration: 2000
        // })
      },

    })
  },
})